package com.sunmi.usercenter.ui.activity;

import java.io.File;
import java.lang.reflect.Field;

import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.controller.FragmentController;
import com.sunmi.usercenter.controller.IFragmentController;
import com.sunmi.usercenter.ui.activity.fragment.InputCodeFragment;
import com.sunmi.usercenter.ui.activity.fragment.LoginFragment;
import com.sunmi.usercenter.ui.activity.fragment.RegisterFragment;
import com.sunmi.usercenter.ui.activity.fragment.RetrievePsdFragment;
import com.sunmi.usercenter.ui.activity.fragment.SettingPsdFragment;
import com.sunmi.usercenter.ui.activity.fragment.StartRegisterFragment;
import com.sunmi.usercenter.ui.activity.fragment.UserAcountFragment;
import com.sunmi.usercenter.ui.activity.fragment.UserAllInfoFragment;
import com.sunmi.usercenter.ui.activity.fragment.UserUpdatePasswordFragment;
import com.sunmi.usercenter.ui.view.IMMListenerRelativeLayout;
import com.sunmi.usercenter.ui.view.IMMListenerRelativeLayout.InputWindowListener;
import com.sunmi.usercenter.utils.CharUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 项目名称：Sunmi-User-Center
 * 类描述：
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/6/8 10:39
 * 修改人：longx
 * 修改时间：2016/6/8 10:39
 * 修改备注：
 */
public class HomeActivity extends SuperFragmentActivity implements IFragmentController, OnClickListener,InputWindowListener{
	
	private FragmentController mFragmentController;
	private FragmentManager fm;
	private RelativeLayout mRelStatus;
	private IMMListenerRelativeLayout mIMMListenerRelativeLayout;
	
	
    @SuppressLint("NewApi")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		// String custom_launcher = getPackageName();
		String custom_launcher = android.provider.Settings.Global.getString(getApplicationContext().getContentResolver(), "custom_launcher");
		if (CharUtils.isEmpty(custom_launcher)) {
			if (custom_launcher.equals("com.sunmi.usercenter")) {
				Constant.ME_IS_LAUNCHER = true;
				title();
				hideNavigation();
			}
		} else {
			Constant.ME_IS_LAUNCHER = false;
			immerseNotificationBar();
			Settings.Secure.putInt(getContentResolver(), "usercenter_show", 1);
		}
        setContentView(R.layout.activity_home);
        mRelStatus = (RelativeLayout) this.findViewById(R.id.sta);
        mIMMListenerRelativeLayout = (IMMListenerRelativeLayout) this.findViewById(R.id.home_layout);
        fm = getSupportFragmentManager();
        mFragmentController = FragmentController.getInstance(this, fm);
        Button button = (Button)this.findViewById(R.id.open);
        button.setOnClickListener(this);
       if(Constant.ME_IS_LAUNCHER){
    	   mRelStatus.setVisibility(View.GONE);
    	   openFM(new FmInfo(StartRegisterFragment.class,null));
    	   mIMMListenerRelativeLayout.setListener(this);
       }else{
    	   openFM(new FmInfo(LoginFragment.class,null));
       }
    }
	private long exitTime = 0;
	
    @Override
    public void onBackPressed() {
    	if(Constant.ME_IS_LAUNCHER){
    		return;
    	}
    	switch (Constant.CLASS_FRAGMENT) {
		case Constant.InputCodeFragment:
			openFM(new FmInfo(InputCodeFragment.class, null));
			break;
		case Constant.LoginFragment:
			openFM(new FmInfo(LoginFragment.class, null));
			break;
		case Constant.RegisterFragment:
			openFM(new FmInfo(RegisterFragment.class, null));
			break;
		case Constant.RetrievePsdFragment:
			openFM(new FmInfo(RetrievePsdFragment.class, null));
			break;
		case Constant.SettingPsdFragment:
			openFM(new FmInfo(SettingPsdFragment.class, null));
			break;
		case Constant.StartRegisterFragment:
			openFM(new FmInfo(StartRegisterFragment.class, null));
			break;
		case Constant.UserAcountFragment:
			openFM(new FmInfo(UserAcountFragment.class, null));
			break;
		case Constant.UserUpdatePasswordFragment:
			openFM(new FmInfo(UserUpdatePasswordFragment.class, null));
			break;
		case Constant.UserAllInfoFragment:
			openFM(new FmInfo(UserAllInfoFragment.class, null));
			break;
		case Constant.DoubleClickExit:
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				exitTime = System.currentTimeMillis();
				Toast.makeText(getApplicationContext(), getResources().getText(R.string.txt_exit), Toast.LENGTH_SHORT)
						.show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
			}
			break;
		default:
			break;
		}
    	
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.open:
			openFM(new FmInfo(StartRegisterFragment.class,null));
			break;

		default:
			break;
		}
	}


	public void SetStatusColorf5f5f5(){
		mRelStatus.setBackgroundResource(R.color.bg);
	}
	
	public void SetStatusColorFE542B(){
		mRelStatus.setBackgroundResource(R.color.CFE542B);
	}
	
	@SuppressLint({ "InlinedApi", "NewApi" })
	private void immerseNotificationBar(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		getWindow().setStatusBarColor(getResources().getColor(R.color.transparent));
	}
	
	private void title(){
		//隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window=HomeActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
	}
	
	@Override
	public void openFM(FmInfo fmInfo) {
		mFragmentController.openFM(fmInfo);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(Constant.ME_IS_LAUNCHER){
			hideNavigation();
		}
	}


	/**
	 * 隐藏虚拟按键
	 */
	@SuppressLint("InlinedApi")
	private void hideNavigation() {
		View view = getWindow().getDecorView();
		view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |View.SYSTEM_UI_FLAG_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | 0x00002000);
	}

	
	@Override
	public void hideSoftInput(Context context, View achor) {
		
	}

	@Override
	public void loadIMG(ImageView img, File file) {
		
	}

	@Override
	public void isShow() {
		new Thread() {
			public void run() {
				try {
					Thread.sleep(1);
					runOnUiThread(new Runnable() {
						public void run() {
							hideNavigation();
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
